package com.charge.util;

import java.security.MessageDigest;

/**
 * Description :
 * Author : 91658
 * Date : 2018/6/7 11:41
 **/
public class MD5Utils {

    public static String encodeByMd5(String str) {

        //确定计算方法
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] data = md5.digest(str.getBytes("utf-8"));// 转换为MD5码
            return bytesToHexString(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString().toUpperCase();
    }
}
